import {Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema } from '@delon/form';
import {NzModalRef, NzModalService} from "ng-zorro-antd/modal";
import {SystemService} from "../../../sys-setting/services/system.service";
import {InterfaceDockingService} from "../../services/interface-docking.service";

@Component({
  selector: 'app-cuc-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.less']
})
export class settingVehicleEditComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('menu', { static: false })
  roleInfoData: any = {};
  authorityAssistId: any[] = [];
  params: any[] = [];
  changeValue: boolean = false
  schema!: SFSchema;
  authority: any[] = [];
  constructor(private modal: NzModalRef, public service: SystemService, public interfaceService: InterfaceDockingService,) {}


  ngOnInit(): void {
    this.initSF();
  }

  initSF() {
    this.schema = {
      properties: {
        blLoadNo: {
          title: '派车单号',
          type: 'string',
          maxLength: 50,
          default: this.params[0].blLoadNo,
          readOnly: true,
        },
        driverName: {
          title: '司机姓名',
          type: 'string',
          maxLength: 20,
          default: this.params[0].driverName,
          ui: {
            placeholder: '请输入司机姓名'
          }
        },
        deliveryTime: {
          title: '提货时间',
          type: 'string',
          maxLength: 20,
          format: 'date-time',
          default: this.params[0].deliveryTime,
          ui: {
            format: 'yyyy-MM-dd HH:mm:ss',
          }
        },
        licenseNumber: {
          title: '车牌号',
          type: 'string',
          maxLength: 20,
          default: this.params[0].licenseNumber,
          ui: {
            placeholder: '请输入车牌号',
          }
        },
        containerNo: {
          title: '集装箱号',
          type: 'string',
          maxLength: 20,
          default: this.params[0].containerNo,
          ui: {
            placeholder: '请输入集装箱号',
          }
        },
        sealNo: {
          title: '铅封号',
          type: 'string',
          maxLength: 20,
          default: this.params[0].sealNo,
          ui: {
            placeholder: '请输入铅封号',
          }
        },
        mobilePhone: {
          title: '手机号',
          type: 'string',
          maxLength: 20,
          default: this.params[0].mobilePhone,
          ui: {
            placeholder: '请输入手机号',
          }
        },
        contactPhoneNumber: {
          title: '联系电话',
          type: 'string',
          maxLength: 20,
          default: this.params[0].contactPhoneNumber,
          ui: {
            placeholder: '请输入联系电话',
          }
        },
        idCard: {
          title: '身份证',
          type: 'string',
          maxLength: 20,
          default: this.params[0].idCard,
          ui: {
            placeholder: '请输入身份证',
          }
        },
        remarks: {
          title: '备注',
          type: 'string',
          maxLength: 20,
          default: this.params[0].remarks,
          ui: {
            placeholder: '请输入备注',
          }
        }
      },
      required: ['driverName','deliveryTime','licenseNumber','mobilePhone','idCard']
    };
  }

  close() {
    this.modal.destroy();
  }

  //清空车辆信息
  clearInfo() {
    const params: any = {
      ...this.sf?.value,
    };

    let judge = false;
    this.params.forEach(itemParam=>{
      if(itemParam.status != 4) {
        judge = true;
      }
    })
    if(judge) {
      this.service.msgSrv.warning('当前状态不能清除车辆信息！');
      return;
    }
    this.service.request(this.interfaceService.$api_saveVehicleInfo+'?operation=CANCEL', params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('清除成功');
        this.modal.close(true);
      } else {
        this.service.msgSrv.error('清除失败');
      }
    });
  }

  //保存车辆信息
  sure() {
    if(!this.sf.valid) {
      this.service.msgSrv.warning('必填项不能为空！');
      return;
    }
    const params: any = {
      ...this.sf?.value,
    };
    if(this.params[0].containerNo != null) {
      if(this.params[0].sealNo == null || this.params[0].sealNo == '') {
        this.service.msgSrv.warning('输入了集装箱号必须填写铅封号！');
        return;
      }
    }
    if (this.params[0].vehicleId) {
      this.service.msgSrv.warning('该派车号已存在司机信息，不能保存！');
      return;
    }
    this.service.request(this.interfaceService.$api_saveVehicleInfo, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('编辑成功');
        this.modal.close(true);
      } else {
        this.service.msgSrv.error('编辑失败');
      }
    });
  }

}
